/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateOrder.java
 *
 * Created on Mar 12, 2011, 2:14:11 PM
 */
package vn.aptech.project.view;

import vn.aptech.project.control.*;
import vn.aptech.project.model.dto.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mr.Hiep
 */
public class EditOrder extends java.awt.Dialog {

    boolean check;
    int count = 0;
    float total = 0;
    DefaultTableModel dtmt;

    /** Creates new form CreateOrder */
    public EditOrder(java.awt.Frame parent, boolean modal) throws IOException, SQLException, ClassNotFoundException {
        super(parent, modal);
        initComponents();
        orderline_BLL olb = new orderline_BLL();
        Vector vt = olb.selectByOrderid(variable.orderid);
        Vector vtCol = new Vector();
        vtCol.add("Id");
        vtCol.add("Category");
        vtCol.add("Product_name");
        vtCol.add("Unit Price");
        vtCol.add("Quantity");
        dtmt = new DefaultTableModel(null, vtCol) {

            @Override
            public boolean isCellEditable(int rowIndex, int mColIndex) {
                return false;
            }
        };
        tblProductBill.setModel(dtmt);
        SelectProduct();
        txtCust_name.setText(variable.CustomerName);
        txaAddress.setText(variable.CustomerAddress);
        txtContact.setText(String.valueOf(variable.CustomerContact));
        lbTotal.setText(String.valueOf(variable.TotalAmount));
        for (int i = 0; i < vt.size(); i++) {

            String abc = vt.get(i).toString();
            String temp = abc.substring(1, abc.length());
            String temm = temp.substring(0, temp.length() - 1);

            String[] arr = temm.split(", ");
            dtmt.addRow(new Object[0]);
            tblProductBill.setValueAt(arr[0], count, 0);
            tblProductBill.setValueAt(arr[1], count, 1);
            tblProductBill.setValueAt(arr[2], count, 2);
            tblProductBill.setValueAt(arr[3], count, 3);
            tblProductBill.setValueAt(arr[4], count, 4);
            count++;
        }
     this.setTitle("Edit Order");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        bntSearch = new javax.swing.JButton();
        txtProductName = new javax.swing.JTextField();
        txtProductid = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProduct = new javax.swing.JTable();
        bntAdd = new javax.swing.JButton();
        bntRemove = new javax.swing.JButton();
        txtQuantity = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblProductBill = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtCust_name = new javax.swing.JTextField();
        txtContact = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        txaAddress = new javax.swing.JTextArea();
        bntEdit = new javax.swing.JButton();
        bntCancel = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        lbTotal = new javax.swing.JLabel();

        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 24));
        jLabel1.setForeground(new java.awt.Color(0, 0, 204));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Edit Order");

        bntSearch.setFont(new java.awt.Font("Tahoma", 3, 14));
        bntSearch.setForeground(new java.awt.Color(0, 0, 204));
        bntSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Search.png"))); // NOI18N
        bntSearch.setText("Search");
        bntSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntSearchActionPerformed(evt);
            }
        });

        txtProductName.setFont(new java.awt.Font("Tahoma", 2, 11));
        txtProductName.setText("Enter product name");
        txtProductName.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216)));
        txtProductName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtProductNameFocusGained(evt);
            }
        });

        txtProductid.setFont(new java.awt.Font("Tahoma", 2, 11));
        txtProductid.setText("Enter product id");
        txtProductid.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216)));
        txtProductid.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtProductidFocusGained(evt);
            }
        });
        txtProductid.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtProductidKeyTyped(evt);
            }
        });

        tblProduct.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblProduct.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblProductMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblProduct);

        bntAdd.setFont(new java.awt.Font("Tahoma", 3, 14));
        bntAdd.setForeground(new java.awt.Color(0, 0, 255));
        bntAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.png"))); // NOI18N
        bntAdd.setText("Add");
        bntAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntAddActionPerformed(evt);
            }
        });

        bntRemove.setFont(new java.awt.Font("Tahoma", 3, 14));
        bntRemove.setForeground(new java.awt.Color(0, 0, 255));
        bntRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/delete.png"))); // NOI18N
        bntRemove.setText("Remove");
        bntRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntRemoveActionPerformed(evt);
            }
        });

        txtQuantity.setEnabled(false);
        txtQuantity.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtQuantityKeyTyped(evt);
            }
        });

        tblProductBill.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Category", "Product_name", "Unit Price", "Quantity"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblProductBill);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Customer Information"));

        jLabel2.setFont(new java.awt.Font("Tahoma", 3, 12));
        jLabel2.setForeground(new java.awt.Color(0, 0, 255));
        jLabel2.setText("Customer name:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 3, 12));
        jLabel3.setForeground(new java.awt.Color(0, 0, 255));
        jLabel3.setText("Contact_No:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 3, 12));
        jLabel4.setForeground(new java.awt.Color(0, 0, 255));
        jLabel4.setText("Address:");

        txtCust_name.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216)));

        txtContact.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216)));
        txtContact.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtContactKeyTyped(evt);
            }
        });

        txaAddress.setColumns(20);
        txaAddress.setRows(5);
        txaAddress.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216), new java.awt.Color(236, 233, 216)));
        jScrollPane3.setViewportView(txaAddress);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
                    .addComponent(txtContact, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
                    .addComponent(txtCust_name, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCust_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtContact, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bntEdit.setFont(new java.awt.Font("Tahoma", 3, 14));
        bntEdit.setForeground(new java.awt.Color(0, 0, 204));
        bntEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/edit.png"))); // NOI18N
        bntEdit.setText("Edit");
        bntEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntEditActionPerformed(evt);
            }
        });

        bntCancel.setFont(new java.awt.Font("Tahoma", 3, 14));
        bntCancel.setForeground(new java.awt.Color(0, 0, 255));
        bntCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/delete.png"))); // NOI18N
        bntCancel.setText("Cancel");
        bntCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 3, 12));
        jLabel5.setForeground(new java.awt.Color(51, 102, 255));
        jLabel5.setText("Total amount:");

        lbTotal.setText("0");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(105, Short.MAX_VALUE)
                .addComponent(bntAdd)
                .addGap(18, 18, 18)
                .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(bntRemove)
                .addGap(87, 87, 87))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(150, Short.MAX_VALUE)
                .addComponent(bntEdit)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bntCancel)
                .addGap(154, 154, 154))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(85, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(72, 72, 72))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtProductName, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtProductid, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(bntSearch)
                        .addGap(38, 38, 38))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(15, 15, 15))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bntSearch)
                    .addComponent(txtProductid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtProductName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bntRemove)
                    .addComponent(bntAdd))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbTotal)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bntCancel)
                    .addComponent(bntEdit))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    public void SelectProduct() throws IOException {
        try {
            product_BLL pb = new product_BLL();
            Vector vt;
            if (txtProductName.getText().equals("")) {
                txtProductName.setText("Enter product name");
            }
            if (txtProductid.getText().equals("")) {
                txtProductid.setText("Enter product id");
            }
            if (!txtProductName.getText().equals("Enter product name") && !txtProductid.getText().equals("Enter product id")) {
                vt = pb.SearchByIdAndByName1(Integer.parseInt(txtProductid.getText()), txtProductName.getText());
             
            } else if (!txtProductName.getText().equals("Enter product name")) {
                vt = pb.SearchProductByName1(txtProductName.getText());
              
            } else if (!txtProductid.getText().equals("Enter product id")) {
                vt = pb.SearchProductById1(Integer.parseInt(txtProductid.getText()));
           
            } else {
                vt = pb.selectproduct1();
               
            }

            Vector vtCol = new Vector();
            vtCol.add("Id");
            vtCol.add("Category");
            vtCol.add("Product name");
            vtCol.add("Unit price");
            vtCol.add("Quantity");
            DefaultTableModel dtm = new DefaultTableModel(vt, vtCol) {

                @Override
                public boolean isCellEditable(int rowIndex, int mColIndex) {
                    return false;
                }
            };


            tblProduct.setModel(dtm);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    private void bntAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntAddActionPerformed
        check = true;
        int currentline = tblProduct.getSelectedRow();
        if (currentline > -1) {
            int Productid = Integer.parseInt(tblProduct.getValueAt(currentline, 0).toString());
            String Typeinfo = tblProduct.getValueAt(currentline, 1).toString();
            String Productname = tblProduct.getValueAt(currentline, 2).toString();
            int ProQuantity = Integer.parseInt(txtQuantity.getText());
            String ProUnitprice = tblProduct.getValueAt(currentline, 3).toString();
            int Quatity = Integer.parseInt(tblProduct.getValueAt(currentline, 4).toString());
            if (ProQuantity > Quatity || ProQuantity < 1) {
                JOptionPane.showMessageDialog(this, "Confirm Quantity");
                txtQuantity.requestFocus();
                return;
            } else {
                int q = Integer.parseInt(tblProduct.getValueAt(currentline, 0).toString());
                for (int i = 0; i <= tblProductBill.getRowCount() - 1; i++) {
                    if (q == Integer.parseInt(tblProductBill.getValueAt(i, 0).toString())) {
                        check = false;
                    }
                }
                if (check == true) {
                    dtmt.addRow(new Object[0]);
                    tblProductBill.setValueAt(Productid, count, 0);
                    tblProductBill.setValueAt(Typeinfo, count, 1);
                    tblProductBill.setValueAt(Productname, count, 2);
                    tblProductBill.setValueAt(ProUnitprice, count, 3);
                    tblProductBill.setValueAt(ProQuantity, count, 4);
                    float Price = Float.parseFloat(ProUnitprice);
                    float abc = ProQuantity * Price;
                    int Qn = Quatity - ProQuantity;
                    tblProduct.setValueAt(Qn, currentline, 4);
                    total += abc;
                    count++;
                    lbTotal.setText(String.valueOf(total));
                } else {
                    JOptionPane.showMessageDialog(this, "Already exist");
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Select a line");
        }
    }//GEN-LAST:event_bntAddActionPerformed

    private void txtProductidFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtProductidFocusGained
        txtProductid.setText("");
    }//GEN-LAST:event_txtProductidFocusGained

    private void txtProductNameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtProductNameFocusGained
        txtProductName.setText("");
    }//GEN-LAST:event_txtProductNameFocusGained

    private void bntSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntSearchActionPerformed
        try {
            SelectProduct();
        } catch (IOException ex) {
            Logger.getLogger(EditOrder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bntSearchActionPerformed

    private void tblProductMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblProductMouseClicked
        int currentline = tblProduct.getSelectedRow();
        String Quantity = tblProduct.getValueAt(currentline, 4).toString();
        txtQuantity.setEnabled(true);
        txtQuantity.setText(Quantity);

    }//GEN-LAST:event_tblProductMouseClicked

    private void bntRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntRemoveActionPerformed
        int currentline = tblProductBill.getSelectedRow();
        if (currentline > -1) {
            int a = 0;
            int id1 = Integer.parseInt(tblProductBill.getValueAt(currentline, 0).toString());
            for (int i = 0; i <= tblProduct.getRowCount() - 1; i++) {
                int id2 = Integer.parseInt(tblProduct.getValueAt(i, 0).toString());
                if (id1 == id2) {
                    a = i;
                }
            }
            int Q1 = Integer.parseInt(tblProductBill.getValueAt(currentline, 4).toString());
            int Q2 = Integer.parseInt(tblProduct.getValueAt(a, 4).toString());
            int Q3 = Q1 + Q2;
            tblProduct.setValueAt(Q3, a, 4);
            float Price = Float.parseFloat(tblProductBill.getValueAt(currentline, 3).toString());
            int BillQuantity = Integer.parseInt(tblProductBill.getValueAt(currentline, 4).toString());
            float cacu = BillQuantity * Price;
            float total1 = Float.parseFloat(lbTotal.getText());
            total = total1 - cacu;
            lbTotal.setText(String.valueOf(total));
            dtmt.removeRow(currentline);
            count--;
        } else {
            JOptionPane.showMessageDialog(this, "Select a line");
        }
    }//GEN-LAST:event_bntRemoveActionPerformed

    private void bntEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntEditActionPerformed
      try {
            if (tblProductBill.getRowCount() == 0) {
                JOptionPane.showMessageDialog(this, "Please add products");
                return;
            }
            if (txtCust_name.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "Customer name not blank !!!");
                txtCust_name.requestFocus();
                return;
            }
            if (txtContact.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "Contact_No not blank !!!");
                txtContact.requestFocus();
                return;
            }
            if (txaAddress.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "Address not blank !!!");
                txaAddress.requestFocus();
                return;
            }
            tblorder_BLL order = new tblorder_BLL();
            Employee_Detail_BLL edb = new Employee_Detail_BLL();
            Customer_BLL ctmb = new Customer_BLL();
            Customer_DTO ctd = new Customer_DTO();
            tblorder_DTO tod = new tblorder_DTO();
            category_BLL ctb = new category_BLL();
            oderline_DTO old = new oderline_DTO();
            orderline_BLL odlb = new orderline_BLL();
            int stafid = edb.getStaffidByStaffName(variable.StaffName);
            String typeinfo = edb.getStaffDepartByStaffName(variable.StaffName);
            int typeid = ctb.getTypeidBytypeinfo(typeinfo);
            ctd.setCustomerid(order.getCustomerIdByOrderid(variable.orderid));
            ctd.setCust_name(txtCust_name.getText());
            ctd.setAddr(txaAddress.getText());
            ctd.setContactno(Integer.parseInt(txtContact.getText()));
            ctd.setTypeid(typeid);
            ctmb.updateCustomer(ctd);
            int Cusid = ctmb.getCustomerIdByCust_name(txtCust_name.getText());
            tod.setOrderid(variable.orderid);
            tod.setCustomerid(Cusid);
            tod.setTotalAmount(total);
            tod.setStaffid(stafid);
            order.updatettblorder(tod);
            int Ordid = order.getOrderidByCusid(Cusid);
            odlb.deleteorderline(variable.orderid);
            for (int i = 0; i <= tblProductBill.getRowCount() - 1; i++) {
                old.setOrderid(Ordid);
                old.setPrice(Float.parseFloat(tblProductBill.getValueAt(i, 3).toString()));
                old.setProductid(Integer.parseInt(tblProductBill.getValueAt(i, 0).toString()));
                old.setQuantity(Integer.parseInt(tblProductBill.getValueAt(i, 4).toString()));
                odlb.insertorderline(old);
            }
            for (int i = 0; i <= tblProduct.getRowCount() - 1; i++) {
                int Id = Integer.parseInt(tblProduct.getValueAt(i, 0).toString());
                int Quantity = Integer.parseInt(tblProduct.getValueAt(i, 4).toString());
                try {
                    product_BLL pdb = new product_BLL();
                    pdb.updateQuantity(Quantity, Id);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(EditOrder.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(EditOrder.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(EditOrder.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
            JOptionPane.showMessageDialog(this, "Successful");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(EditOrder.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(EditOrder.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(EditOrder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bntEditActionPerformed

    private void bntCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_bntCancelActionPerformed

    private void txtQuantityKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtQuantityKeyTyped
        int key = evt.getKeyChar();
        String st = txtQuantity.getText();
        String stTest = "0123456789";
        if (key != KeyEvent.VK_BACK_SPACE
                && key != KeyEvent.VK_DELETE
                && key != KeyEvent.VK_ENTER) {
            int flag = 0;
            if (stTest.indexOf(evt.getKeyChar()) == -1) {
                flag++;
            }
            if (flag > 0) {
                evt.consume();
            }
        }
    }//GEN-LAST:event_txtQuantityKeyTyped

    private void txtContactKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtContactKeyTyped
        int key = evt.getKeyChar();
        String st = txtContact.getText();
        String stTest = "0123456789";
        if (key != KeyEvent.VK_BACK_SPACE
                && key != KeyEvent.VK_DELETE
                && key != KeyEvent.VK_ENTER) {
            int flag = 0;
            if (stTest.indexOf(evt.getKeyChar()) == -1) {
                flag++;
            }
            if (flag > 0) {
                evt.consume();
            }
        }
    }//GEN-LAST:event_txtContactKeyTyped

    private void txtProductidKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtProductidKeyTyped
    int key = evt.getKeyChar();
        String st = txtProductid.getText();
        String stTest = "0123456789";
        if (key != KeyEvent.VK_BACK_SPACE
                && key != KeyEvent.VK_DELETE
                && key != KeyEvent.VK_ENTER) {
            int flag = 0;
            if (stTest.indexOf(evt.getKeyChar()) == -1) {
                flag++;
            }
            if (flag > 0) {
                evt.consume();
            }
        }        // TODO add your handling code here:
    }//GEN-LAST:event_txtProductidKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    EditOrder dialog = new EditOrder(new java.awt.Frame(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                        @Override
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(EditOrder.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(EditOrder.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(EditOrder.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bntAdd;
    private javax.swing.JButton bntCancel;
    private javax.swing.JButton bntEdit;
    private javax.swing.JButton bntRemove;
    private javax.swing.JButton bntSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lbTotal;
    private javax.swing.JTable tblProduct;
    private javax.swing.JTable tblProductBill;
    private javax.swing.JTextArea txaAddress;
    private javax.swing.JTextField txtContact;
    private javax.swing.JTextField txtCust_name;
    private javax.swing.JTextField txtProductName;
    private javax.swing.JTextField txtProductid;
    private javax.swing.JTextField txtQuantity;
    // End of variables declaration//GEN-END:variables
}
